package android.crosstime.hes;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.crosstime.utils.JUtils;
import android.crosstime.utils.RootCmd;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SlidingDrawer;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import edu.android.hes.R;

public class HExplorer extends Activity{
	
//	private RelativeLayout mMainView = null;
	private RelativeLayout mDialogView = null;
	private ListView mFileListView = null;
	private FileListAdapter mListAdapter = null;
	private LinearLayout mTabContainer = null;
	public static final int MENU_ID_ABOUT = Menu.FIRST+1;
	private SlidingDrawer mSlider = null;
	private HorizontalScrollView mHoriView = null;
	private DragLayer mDragLayer = null;  //for drag operation graphic
	private FloatDialogManager mDialogManager = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
//        if(RootCmd.haveRoot())
//        	Toast.makeText(this, "I have root!", Toast.LENGTH_LONG).show();
//        else
//        	Toast.makeText(this, "I don't have root!", Toast.LENGTH_LONG).show();
        initViews();
        if(mDialogManager == null)
        	mDialogManager = new FloatDialogManager();
    }
    
    private void initViews(){
        mDialogView = (RelativeLayout)findViewById(R.id.DialogLayout);
        mFileListView = (ListView)findViewById(R.id.exFileListView);   
        mTabContainer = (LinearLayout)findViewById(R.id.slide_content);
        mListAdapter = new FileListAdapter(this, mDialogView);
        mFileListView.setAdapter(mListAdapter); 
        mFileListView.setOnItemClickListener(itemClickListener);
        mFileListView.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(mDragLayer != null)
					mDragLayer.switchDragIcon(true);

				return false;
			}});
        mSlider = (SlidingDrawer) findViewById(R.id.slidingContainer);
        mHoriView = (HorizontalScrollView) findViewById(R.id.slide_content_s);
        mDragLayer = (DragLayer) findViewById(R.id.DragLayout);
    }
    
    
    @Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
    	switch(keyCode){
	    	case KeyEvent.KEYCODE_BACK:
	    		boolean ret = false;
	    		if(mListAdapter != null)
	    			ret = mListAdapter.openParentFolder(); //back to parent folder
	    		if(!ret)
	    		{
		    		new AlertDialog.Builder(this).setTitle(R.string.confirm_title)
						.setMessage(R.string.exit_confirm_info).setPositiveButton(
								R.string.yes, new OnClickListener() {
	
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										finish();
									}
								}).setNegativeButton(R.string.no,
								new OnClickListener() {
	
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										
									}
								}).create().show();
	    		}
	    		return true;
	    	case KeyEvent.KEYCODE_MENU:
	    		break;
    	}
		return super.onKeyUp(keyCode, event);
	}

    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(Menu.NONE, MENU_ID_ABOUT, Menu.NONE, R.string.about);
		return super.onCreateOptionsMenu(menu);
	}



	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case MENU_ID_ABOUT:
			new AlertDialog.Builder(this).setTitle(R.string.about)
			.setMessage(R.string.about_info).setNegativeButton(
					R.string.yes, new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog,
								int which) {
							
						}
					}).create().show();
			break;
		}
		return super.onOptionsItemSelected(item);
	}



	private OnItemClickListener itemClickListener = new OnItemClickListener(){

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			
			String newPath = mListAdapter.getFilePathByPosition(position);
			if(mListAdapter.changeDirectory(newPath))
				mFileListView.setAdapter(mListAdapter);
		}    
    	
    };
    
    public RelativeLayout getDialogLayout(){
    	return mDialogView;
    }
    
    public void addToContainer(FloatDialog dialog){
    	if(mTabContainer == null)
    		return;
    	final Button btn = new Button(this);
    	btn.setWidth(60); btn.setHeight(30);
    	btn.setTag(dialog);
    	btn.setSingleLine();
    	btn.setEllipsize(TruncateAt.END);
    	btn.setBackgroundResource(R.drawable.tab_item_bg);
    	btn.setText(dialog.getCurrentDir().getName().equals("")? "/":dialog.getCurrentDir().getName());
    	btn.setTextColor(0xffffffff);
    	btn.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				if(mDialogManager == null)
					return;
				FloatDialog dialog = (FloatDialog) btn.getTag();
				String path = dialog.getCurrentDir().getAbsolutePath();
				FloatDialog view = mDialogManager.createNewDialog(HExplorer.this, path);
				RelativeLayout.LayoutParams ly = new RelativeLayout.LayoutParams(300, 300);
				ly.leftMargin = 60;
				ly.topMargin = 100;
				mDialogView.addView(view, ly);
				mTabContainer.removeView(btn);
			}});
    	btn.setOnLongClickListener(new OnLongClickListener(){

			@Override
			public boolean onLongClick(View v) {
				mTabContainer.removeView(btn);
				if(mDialogManager != null){
					mDialogManager.remove((FloatDialog) btn.getTag());
				}
				return true;
			}});
    	mTabContainer.addView(btn);
    	
    }
    
    //check if touch point is in the slide container
    //if so, add this dialog to container, return true;
    public boolean checkIfInContainer(int x, int y, FloatDialog dialog){
    	Rect outRect = new Rect();

    	if(!mSlider.isOpened())
    		return false;
    	mHoriView.getGlobalVisibleRect(outRect); //good fix
    	if(outRect.contains(x, y))
    	{
    		addToContainer(dialog);
    		return true;
    	}else
    		return false;
    	
    }
}